How to convert a Seq[A] to a Map[Int, A] using a value of A as the key in the map?

Posted by Jesper on Stack Overflow See other posts from Stack Overflow or by Jesper
Published on 2010-05-27T21:22:25Z Indexed on 2010/05/27 22:11 UTC
Read the original article Hit count: 350

Filed under:
|
|

I have a Seq containing objects of a class that looks like this:

class A (val key: Int, ...)

Now I want to convert this Seq to a Map, using the key value of each object as the key, and the object itself as the value. So:

val seq: Seq[A] = ...
val map: Map[Int, A] = ... // How to convert seq to map?

How can I does this efficiently and in an elegant way in Scala 2.8?

© Stack Overflow or respective owner

Related posts about scala

Related posts about collections